package com.maybach.core.manager.impl;

import com.maybach.common.model.IdentifyDO;
import com.maybach.common.query.IdentifyQuery;
import com.maybach.core.dal.mapper.ext.IdentifyExtMapper;
import com.maybach.core.manager.IdentifyManager;
import com.maybach.util.query.support.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * Manager for Identify.
 */

@Component
public class IdentifyManagerImpl implements IdentifyManager{
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    
    @Autowired
    protected IdentifyExtMapper identifyExtMapper;
    /**
     * query count by query condition.
     */
    @Override
    public int countByQuery(IdentifyQuery query){
        return identifyExtMapper.countByQuery(query);
    }

    /**
     * delete by query condition.
     */
    @Override
    public int deleteByQuery(IdentifyQuery query){
        return identifyExtMapper.deleteByQuery(query);
    }

    /**
     * delete by primary key.
     */
    @Override
    public int deleteByPrimaryKey(IdentifyDO record){
        return identifyExtMapper.deleteByPrimaryKey(record);
    }

    /**
     * insert selective.
     */
    @Override
    public long insertSelective(IdentifyDO record){
        return identifyExtMapper.insertSelective(record);
    }

    /**
     * select by query condition.
     */
    @Override
    public List<IdentifyDO> selectByQuery(IdentifyQuery query){
        return identifyExtMapper.selectByQuery(query);
    }

    /**
     * select by query condition with page.
    */
    @Override
    public PageResult<IdentifyDO> selectByQueryWithPage(IdentifyQuery query) {
        PageResult<IdentifyDO> result = new PageResult<IdentifyDO>();
        result.setPageSize(query.getPageSize());
        result.setPageNo(query.getPageNo());
        result.setTotalCount(this.countByQuery(query));
        result.setResult(this.selectByQuery(query));
        return result;
    }

    /**
     * select by primary key.
     */
    @Override
    public IdentifyDO selectByPrimaryKey(Long id){
        return identifyExtMapper.selectByPrimaryKey(id);
    }

    /**
     * update by query condition selective.
     */
    @Override
    public int updateByQuerySelective( IdentifyDO record,  IdentifyQuery query){
        return identifyExtMapper.updateByQuerySelective(record,  query);
    }

    /**
     * update by query condition.
     */
    @Override
    public int updateByQuery( IdentifyDO record,  IdentifyQuery query){

        return identifyExtMapper.updateByQuery(record, query);
    }

    /**
     * update by primary key selective.
     */
    @Override
    public int updateByPrimaryKeySelective(IdentifyDO record){
        return identifyExtMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Long createAndGetIdentify(String code) {
        Long value = identifyExtMapper.getValueByType(code);
        identifyExtMapper.increaseIdentify(code);
        Long newValue = identifyExtMapper.getValueByType(code);
        if(newValue - value == 1){
            return newValue;
        }else {
            throw new RuntimeException("获取失败[code="+code+"]");
        }

    }
}